package com.tommy.jiami.view;

import android.content.*;
import android.database.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import com.tommy.jiami.adapter.*;
import android.provider.*;
import java.util.*;
import com.tommy.jiami.utils.*;
import java.io.*;
import com.tommy.jiami.utils.task.*;

public class MediaView extends GridView implements OnItemClickListener
{
	public static final int TYPE_PICTURE=0;
	public static final int TYPE_AUDIO=1;
	public static final int TYPE_VIDEO=2;
	public static final int TYPE_ENCRYPTED=3;

	private int mediaType;
	private MediaAdapter adapter;
	private boolean isInit=false;
	private int width=-1;

	public MediaView(Context context,int mediaType){
		super(context);
		this.mediaType=mediaType;
		int numColumns=Utils.getScreenWidth()/Constants.DefaultMediaWidth;
		setNumColumns(numColumns>=2?numColumns:2);
	}

	public List<Media> getMedias()
	{
		return adapter.getMedias();
	}

	public Media getMedia(int position)
	{
		return adapter.getMedias().get(position);
	}
	
	public void init(){
		if(!isInit){
		GetMediaTask task=new GetMediaTask(getContext()){
			public void onGetResult(List<Media> medias){
				adapter=new MediaAdapter(getContext(),mediaType,medias);
				adapter.setMediaWidth(width);
				setAdapter(adapter);
				setOnItemClickListener(MediaView.this);
				isInit=true;
			}
		};
		task.execute(mediaType);
		}
	}

	public void reset(){
		GetMediaTask task=new GetMediaTask(getContext()){
			public void onGetResult(List<Media> medias){
				adapter.changeMedias(medias);
			}
		};
		task.execute(mediaType);
	}

	@Override
	public void setNumColumns(int numColumns)
	{
		super.setNumColumns(numColumns);
		width=Utils.getScreenWidth()/numColumns;
	}
	
	
	
	@Override
	public void onItemClick(AdapterView<?> adapterView, View convertView, int position, long l)
	{
		
	}
	
}
